package com.ebs.md.api.comparators;

/**
 * Revision Author:
 *  $File: //depot/MarketData/commons-md-api/MAIN/src/main/java/com/ebs/md/api/comparators/BasicBookEntryComparator.java $
 *  $Revision: #1 $
 *  $DateTime: 2012/04/29 09:26:44 $
 *  $Change: 110636 $
 *  $Author: Arik.Sher $
 */

import java.util.Comparator;

import com.ebs.md.api.BookSide;
import com.ebs.md.api.IBookEntry;


public class BasicBookEntryComparator
implements Comparator<IBookEntry>
{
	
	// null values should come last.
	public int compare(IBookEntry be1, IBookEntry be2) {
		if ( be1 == null ) {
			if ( be2 == null ) {
				return  0;
			} else {
				return 1;
			}
		}
		if ( be2 == null ) {
			return -1;
		}
		
		BookSide theSide = be1.getSide();
		if ( ! theSide.equals(be2.getSide()) ) {
			throw new RuntimeException(String.format("cannot compare different sides. 1 is %s, and 2 is %s", theSide, be2.getSide()));
		}
		
		if ( BookSide.BID.equals(theSide) ) {
			return MdComparators.  bidRateComperator.compare(be1.getRate(), be2.getRate());
		} else {
			return MdComparators.offerRateComperator.compare(be1.getRate(), be2.getRate());
		}
	}

}
